<?php
class payUComponents extends sfComponents 
{
    public function executeForm(sfWebRequest $request)
    {	
        $this->form = new PayUForm();
        $this->form->setForUser($this->getUser()->getGuardUser());
        $this->redirect = false;
        
        if ($request->isMethod(sfRequest::POST)) {
            $valuesToBind = array_merge($request->getParameter('platnosci'),array('pay_type'=>$request->getParameter('pay_type')));
            $this->form->bind($valuesToBind);
            if ($this->form->isValid())
            {
                // zapisz zamówienie do tabeli               
                $object = $this->product_name;
                $product = call_user_func($object.'Peer::retrieveByPK',$this->product_id);
                
                $sessionId = uniqid();
                $order = new Orders();
                $order->setSessionId($sessionId);
                $order->setUserId($this->getUser()->getUserId());
                $order->setUserIp($this->form->getValue('client_ip'));
                $order->setTax(23);
                $order->setTotalPrice($product->getPrice());
                if ($this->form->getValue('rabat')) {
                    $order->setDiscountId(1);
                    $order->setTotalDiscount(DiscountPeer::calculateDiscount($order->getDiscountId(),$order->getTotalPrice()));
                } else {
                    $order->setTotalDiscount(0);
                }
                $order->setTotalTax(($order->getTotalPrice()-$order->getTotalDiscount())*$order->getTax()/100);
                $order->setTotalRealPrice($order->getTotalPrice()-$order->getTotalDiscount()-$order->getTotalTax());
                $order->setProductName($this->product_name);
                $order->setProductId($this->product_id);
                $order->save();

                // utwórz specjalny formularz
                $payUArray = sfConfig::get('app_payment_method_platnosci');
                $form = new PayUForm(array(),array('CSRFProtection'=>false));
                $form->setPlatnosci($this->form,
                        $payUArray['pos_id'],
                        $payUArray['pos_auth_key'],
                        $order->getTotalRealPrice(true),
                        $sessionId,
                        $this->product_name,
                        $payUArray['key_1']);

                $this->form = $form;
                $this->redirect = true;
            }            
        }  
    }
}